package org.emet.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.emet.entity.biz.Cremator;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.CrematorCriteria;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CrematorDao extends ArchDataSourceBaseDao<Cremator, Long> {
	private static final String crematorEntityName = Cremator.class.getSimpleName();

	public Page<Cremator> search(Page<Cremator> page, CrematorCriteria criteria) {
		Criteria c = getSession().createCriteria(Cremator.class);
		NonEmptyValueHashMap<String, Object> propertyNameValues = new NonEmptyValueHashMap<String, Object>();
		propertyNameValues.put("name", criteria.getName());
		propertyNameValues.put("status", criteria.getStatus());
		if (!StringUtils.isBlank(criteria.getRemark())) {
			c.add(Restrictions.like("remark", "%" + criteria.getRemark() + "%"));
		}
		c.add(Restrictions.allEq(propertyNameValues));
		page.setOrderBy("id");
		page.setOrder("desc");
		return findPage(page, c);
	}

	public List<Long> getAllIds() {
		String hql = "select id from " + crematorEntityName;
		return find(hql);
	}

	public String getStatus(Long id) {
		return findUnique("select status from " + crematorEntityName + " where id=?", id);
	}
}
